package com.glorysoft.sunnypda.ui.updateIp

import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.FabEntity
import com.glorysoft.sunnypda.entity.LineEntity
import com.glorysoft.sunnypda.entity.PositionEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.google.gson.JsonObject

/**
 * FileName: UpdateIpViewModel
 * Author: XJH
 * Date: 2022/1/11 18:08
 * Description:
 * History:
 */
class UpdateIpViewModel : BaseViewModel() {

    val eqpRepository : EqpRepository by lazy { EqpRepository() }
    var mEntity : EqpInfoEntity? = null
    var fabEntityList: ArrayList<FabEntity> = arrayListOf()
    var lineEntityList: ArrayList<LineEntity> = arrayListOf()
    var posEntityList: ArrayList<PositionEntity> = arrayListOf()
    var lineList = arrayListOf<String>()
    var posList = arrayListOf<String>()
    var eqpTypeList = arrayListOf<String>()

    fun getEqpInfo(eqp : String){
        val map = JsonObject()
        map.addProperty("EQPID",eqp)
        launchOnlyResult({
            val entity = eqpRepository.getEqpInfo(map)
            mEntity = entity
            entity.EQPTYPELIST.forEach{
                eqpTypeList.add(it.EQPTYPE!!)
            }
            defUI.callObserve.value = CallObserveBean(0,entity)
        })
    }

    fun updateIP (eqp : String,ip : String,type : String,port : String,line : String,position : String){
//        var lineID = ""
//        var positionID = ""
//        lineEntityList.forEach {
//            if (line == it.LINE){
//                lineID = it.LINEID
//                it.POSITIONLIST.forEach {
//                    if (position == it.POSITION) {
//                        positionID = it.POSITIONID
//                    }
//                }
//            }
//        }
        val map = JsonObject()
        map.addProperty("EQPID",eqp)
        map.addProperty("EQPIP",ip)
        map.addProperty("EQPTYPE",type)
        map.addProperty("EQPPORT",port)
        map.addProperty("FAB", mEntity!!.FAB)
        map.addProperty("FABID", mEntity!!.FABID)
        map.addProperty("LINE", if (line == "")"" else line.split(":")[1])
        map.addProperty("LINEID", if (line == "")"" else line.split(":")[0])
        map.addProperty("POSITION", if (position == "")"" else position.split(":")[1])
        map.addProperty("POSITIONID", if (position == "")"" else position.split(":")[0])
        launchOnlyResult({
            eqpRepository.updateIP(map)
            defUI.startActivity.call()
        })
    }

    fun getFabList() {
        val map = JsonObject()
        launchOnlyResult({
            val allFabLinePosition = eqpRepository.getAllFabLinePosition(map)
            fabEntityList.clear()
            fabEntityList.addAll(allFabLinePosition.FABLIST)
            getLineList(mEntity!!.FAB!!)
            if (mEntity!!.LINE != "") {
                getPositionList(mEntity!!.LINE!!)
            }
        })
    }

    fun getLineList(fab: String) {
        lineEntityList.clear()
        lineList.clear()
        posList.clear()
        lineList.add("")
        fabEntityList.forEach {
            if (it.FAB == fab) {
                lineEntityList.addAll(it.LINELIST)
            }
        }
        lineEntityList.forEach {
            lineList.add(it.LINEID+":"+it.LINE)
        }
    }

    fun getPositionList(line: String) {
        posList.clear()
        posEntityList.clear()
        posList.add("")
        lineEntityList.forEach {
            if (it.LINE == line) {
                it.POSITIONLIST.forEach {
                    posList.add(it.POSITIONID+":"+it.POSITION)
                }
            }
            posEntityList.addAll(it.POSITIONLIST)
        }
    }

}